import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_admin/api/odoo/stores_requisition_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/models/odoo/stores_requisition_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:intl/intl.dart';

class StoresRequisitionQuantity extends StatefulWidget {
  StoresRequisitionQuantity({this.stockQuant,this.parentId});

  final StockQuant? stockQuant;
  final String? parentId;

  @override
  _StoresRequisitionQuantityState createState() =>
      _StoresRequisitionQuantityState();
}

class _StoresRequisitionQuantityState extends State<StoresRequisitionQuantity> {
  StoresRequisitionInfo storesRequisitionInfo = StoresRequisitionInfo();
  num quantity = 0;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  DateFormat format = DateFormat('yyyy-MM-dd');
  List<SelectOptionVO> workshopIdList = [];

  @override
  void initState() {
    super.initState();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    quantity = widget.stockQuant!.quantity!;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '库房位置',
            enable: false,
            value: widget.stockQuant!.locationName,
          ),
          CryInput(
            label: '物料编码',
            enable: false,
            value: widget.stockQuant!.productCode,
          ),
          CryInput(
            label: '物料名称',
            enable: false,
            value: widget.stockQuant!.productName,
          ),
          CryInput(
            label: '规格型号',
            enable: false,
            value: widget.stockQuant!.productDes,
          ),
          CryInputNum(
            label: '数量',
            value: quantity,
            onSaved: (v) {
              quantity = v!;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '单位',
            enable: false,
            value: widget.stockQuant!.productUomName,
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            storesRequisitionInfo.parentId = widget.parentId;
            storesRequisitionInfo.quantity = quantity;
            storesRequisitionInfo.productId = widget.stockQuant!.productId;
            StoresRequisitionOdooApi.confirmUpdata(
                    storesRequisitionInfo.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text('数量确认'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
